﻿using System;
using Loki.Model;
using Luna.Data.CRM;

namespace Luna.Model.CRM
{
    public class LunaCRMService : LunaService, ICRMService
    {
        private EntityContainer<Account, Guid> accounts;

        private EntityContainer<Category, Guid> categories;

        private EntityContainer<Contact, Guid> contacts;

        public LunaCRMService()
        {
            accounts = CreateContainer<Account, Guid>(CacheConfiguration.ACCOUNTS);
            categories = CreateContainer<Category, Guid>(CacheConfiguration.CATEGORIES);
            contacts = CreateContainer<Contact, Guid>(CacheConfiguration.CONTACTS);
        }

        public IIndexedEnumerable<IAccount, Guid> Accounts
        {
            get { return accounts; }
        }

        public IIndexedEnumerable<ICategory, Guid> Categories
        {
            get { return categories; }
        }

        public void Delete(IEditableAccount account)
        {
            Delete<EditableAccount, Account, IEditableAccount, IAccountData>(account, DataContext.Contacts.AccountDelete, accounts);
        }

        public void Delete(IEditableCategory category)
        {
            Delete<EditableCategory, Category, IEditableCategory, ICategoryData>(category, DataContext.Contacts.CategoryDelete, categories);
        }

        public void Reset()
        {
            categories.Reset();
            accounts.Reset();
            contacts.Reset();
        }

        public void Save(IEditableAccount account)
        {
            Save<EditableAccount, Account, IEditableAccount, IAccountData>(account, DataContext.Contacts.AccountSave, accounts);
        }

        public void Save(IEditableCategory category)
        {
            Save<EditableCategory, Category, IEditableCategory, ICategoryData>(category, DataContext.Contacts.CategorySave, categories);
        }

        public IIndexedEnumerable<IContact, Guid> Contacts
        {
            get { return contacts; }
        }

        public void Save(IEditableContact contact)
        {
            Save<EditableContact, Contact, IEditableContact, IContactData>(contact, DataContext.Contacts.ContactSave, contacts);
        }

        public void Delete(IEditableContact contact)
        {
            Save<EditableContact, Contact, IEditableContact, IContactData>(contact, DataContext.Contacts.ContactDelete, contacts);
        }
    }
}